package com.gok.seasonmovie.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class HttpUtil {

    public static Map<String, Object> get(String url) {
        Map<String, Object> result = new HashMap<>();

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(url);//请求对象
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                String json = EntityUtils.toString(response.getEntity());//响应实体变字符串
                ObjectMapper mapper = new ObjectMapper();
                result = mapper.readValue(json, Map.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}